#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# brevis.one SMS Gateway

# Send notification to a brevis.one SMS Gateway
#
# This notification script can be put below share/check_mk/notifications.
# Please configure the needed credentials

import os
import sys
import urllib.parse
import urllib.request

# user with permission to use Web-API of gateway
username = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
password = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
# location (http://servername/api.php)
location = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"

to = os.environ.get("NOTIFY_CONTACTPAGER")
message = os.environ["NOTIFY_HOSTNAME"] + " "

if os.environ["NOTIFY_WHAT"] == "SERVICE":
    message += os.environ["NOTIFY_SERVICESTATE"] + " "
    message += os.environ["NOTIFY_SERVICEDESC"]
else:
    # Host message
    message += "is " + os.environ["NOTIFY_HOSTSTATE"]

url = "%s?" % location + urllib.parse.urlencode(
    [("username", username), ("password", password), ("to", to), ("text", message)]
)

try:
    handle = urllib.request.urlopen(url)
    response = handle.read().strip()

    if handle.getcode() == 200:
        sys.stdout.write("Successfully sent SMS to %s\n" % to)
    else:
        sys.stderr.write("Error sending SMS to %s: HTTP error code %s\n" % (to, handle.getcode()))
        sys.stderr.write("URL was %s\n" % url)
except Exception as e:
    sys.stderr.write("Error sending SMS to %s: %s\n" % (to, e))
